package com.jackwei.leetcode.offer.sword;

import java.util.Scanner;

/**
 * 剑指 Offer 06. 从尾到头打印链表
 *
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。

 * 示例 1：
 *
 * 输入：head = [1,3,2]
 * 输出：[2,3,1]
 *  
 *
 * 限制：
 *
 * 0 <= 链表长度 <= 10000
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class O006 {
}

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
 }
class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode index = head;
        int size = 0;
        while (index != null) {
            size++;
            index = index.next;
        }
        int[] result = new int[size];
        int i = size;
        while (head != null) {
            result[--i] = head.val;
            head = head.next;
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String inputStr = scanner.next();
        // 去头去尾，替换“并处理成数字加逗号隔开
        inputStr = inputStr.substring(1, inputStr.length() - 1);
        String[] nums = inputStr.split(",");
        ListNode head = new ListNode(Integer.parseInt(nums[0]));
        ListNode index = head;
        for (int i = 1; i < nums.length; i++) {
            ListNode listNode = new ListNode(Integer.parseInt(nums[i]));
            index.next = listNode;
            index = listNode;
        }
        Solution solution = new Solution();
        int[] ints = solution.reversePrint(head);
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int n : ints){
            sb.append(n+",");
        }
        String re = sb.substring(0, sb.length() - 1);
        System.out.println(re + "]");
    }
}